package com.tju.resource.oauth.controller.tb;

import com.tju.resource.annotation.Log;
import com.tju.resource.business.tb_app_log.TbAppLogService;
import com.tju.resource.business.tb_purchase.TbPurchaseService;
import com.tju.resource.model.TbPurchase;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.enum_utils.FunctionType;
import com.tju.resource.tool.tool.ClientParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;
import java.util.Objects;

/**
 * 采购管理接口
 */
@RestController
public class TbPurchaseController {
    private final String prefix = "purchase";
    @Autowired
    TbPurchaseService tbPurchaseService;
    @Autowired
    private TbAppLogService tbAppLogService;

    /**
     * 查询
     *
     * @param parameters
     * @return
     */
    @PreAuthorize("hasAuthority('sys:purchase:purchase:list')")
    @PostMapping(prefix + "/get")
    @Log(type = FunctionType.List)
    public Result<Object> get(@RequestBody ClientParameters parameters) {
        return tbPurchaseService.get(parameters);
    }

    /**
     * 添加
     *
     * @param tbPurchase
     * @return
     */
    @PreAuthorize("hasAuthority('sys:purchase:purchase:add')")
    @PostMapping(prefix + "/add")
    @Log(type = FunctionType.Add)
    public Result<Object> add(@Valid @RequestBody TbPurchase tbPurchase, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        return tbPurchaseService.add(tbPurchase);
    }

    /**
     * 删除
     *
     * @param tbPurchase
     * @return
     */
    @PreAuthorize("hasAuthority('sys:purchase:purchase:delete')")
    @PostMapping(prefix + "/del")
    @Log(type = FunctionType.Delete)
    public Result<Object> del(@RequestBody TbPurchase tbPurchase) {
        return tbPurchaseService.del(tbPurchase);
    }

    /**
     * 修改
     *
     * @param tbPurchase
     * @return
     */
    @PreAuthorize("hasAuthority('sys:purchase:purchase:edit')")
    @PostMapping(prefix + "/upd")
    @Log(type = FunctionType.Modify)
    public Result<Object> upd(@Valid @RequestBody TbPurchase tbPurchase, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        return tbPurchaseService.upd(tbPurchase);
    }

    /**
     * 审核采购单
     * @param tbPurchase
     * @return
     */
    @PreAuthorize("hasAuthority('sys:purchase:purchase:approve')")
    @PostMapping(prefix+"/check")
    @Log(type = FunctionType.FirstInstance)
    public Result<Object> check(@RequestBody TbPurchase tbPurchase){
        return tbPurchaseService.check(tbPurchase);
    }
    /**
     * 审核采购单
     * @param tbPurchase
     * @return
     */
    @PreAuthorize("hasAuthority('sys:purchase:purchase:twoApprove')")
    @PostMapping(prefix+"/twoCheck")
    @Log(type = FunctionType.SecondInstance)
    public Result<Object> twoCheck(@RequestBody TbPurchase tbPurchase){
        return tbPurchaseService.twoCheck(tbPurchase);
    }

    /**
     * 查询审核记录
     */
    @PostMapping(prefix + "/selectApprove")
    @PreAuthorize("hasAuthority('sys:purchase:purchase:auditRecords')")
    @Log(type = FunctionType.List)
    public Result<Object> selectApprove(@RequestBody Map<String,String> map) {
        return tbAppLogService.selectTbAppLogByPurchaseId(map);
    }
}
